/**
 * 信息输入控件
 */
cc.Class({
    extends: cc.Component,

    properties: {
        body: {
            default: null,
            type: cc.Node,
            tooltip: '整个输入控件的容器',
        },
        hideOnOk: {
            default: false,
            tooltip: '是否点击ok时自动隐藏',
        },
        clearOnOk: {
            default: false,
            tooltip: '确认输入后，清空内部数据',
        },
        clearOnOpen:{
            default: true,
            tooltip:'打开键盘时，是否清空以前的输入',
        },
        input: {
            default: '',
            tooltip: '当前输入的信息',
        },
        maxLength: {
            default: -1,
            type: cc.Integer,
            tooltip: '最大输入字符数量',
        },
        inputEvents: {
            default: [],
            type: [cc.Component.EventHandler],
            tooltip: '输入时会调用，传入当前值和积累值'
        },
        okEvents: {
            default: [],
            type: [cc.Component.EventHandler],
            tooltip: '完成输入时调用，传入积累值'
        },
    },
    start(){
        if (this.clearOnOpen === true)
            this.onKey(null, 'clear');
    },
    /**
     * 
     * @param {} e 
     * @param {*} key 按键传进来的信息，一般是一个字符
     */
    onKey(e, key) {
        if (key === 'ok') {
            // 将输入赋值给回调
            cc.Component.EventHandler.emitEvents(this.okEvents, new cc.Event.EventCustom('ok'), this.input);
            if (this.onOK)
                this.onOK(this, this.input);
            //
            if (this.hideOnOk === true)
                this.show(false);
            if (this.clearOnOk === true)
                this.clear();
        } else if (key === 'back') {
            this._back();
            cc.Component.EventHandler.emitEvents(this.inputEvents, new cc.Event.EventCustom('input'), this.input);
            if (this.onInput)
                this.onInput(this, this.input);
        } else if (key === 'clear') {
            this.clear();
            cc.Component.EventHandler.emitEvents(this.inputEvents, new cc.Event.EventCustom('input'), this.input);
            if (this.onInput)
                this.onInput(this, this.input);
        } else {
            //
            this.input += key;
            // 根据字数限制截取
            if (this.maxLength > 0 && this.input.length > this.maxLength) {
                this.input = this.input.slice(this.input.length - this.maxLength);
            }
            //
            cc.Component.EventHandler.emitEvents(this.inputEvents, new cc.Event.EventCustom('input'), this.input, key);
            if (this.onInput)
                this.onInput(this, this.input, key);
        }
    },
    /**
     * 设置显示或隐藏
     * @param {*} b 
     */
    show(b) {
        this.body.active = (b === false) ? false : true;
    },
    /**
     * 隐藏
     */
    hide() {
        this.show(false);
    },
    /**
     * 清空信息
     */
    clear() {
        this.input = '';
    },
    _back() {
        if (this.input.length === 0)
            return;
        else if (this.input.length === 1)
            this.input = '';
        else
            this.input = this.input.substring(0, this.input.length - 1);
    }
});
